<div class="loading-container"><div class="loader"></div></div>

<div class="wrapper wrapper-radius wrapper-dashboard wrapper-transparent">
	<!-- list data wrapper -->
	<div class="result-wrapper">
   	<div class="content-wrapper">
     	<div class="content-body">
     		<!-- 本月客户数统计 -->
     		<div class="row">
   					<!-- 本月新增客户 -->
   					<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
             <div class="databox box-shadow bg-white radius-bordered">
               <div class="databox-left bg-orange">
                 <div class="databox-piechart">
                  	<div class="easypiechart" data-percent="0" data-size="47" data-animate="500">
               			 		<span class="white font-90" data-epc-percent="本月新增客户">0%</span>
                  	</div>
                 </div>
               </div>
               <div class="databox-right">
                 <span class="databox-number orange" data-epc-count="本月新增客户">0</span>
                 <div class="databox-text darkgray">本月新增客户</div>
                 <div class="databox-stat orange radius-bordered">
                  	<i class="stat-icon icon-lg fa fa-user-plus"></i>
                 </div>
               </div>
             </div>
       		</div>
       		<!-- 本月跟进客户 -->
       		<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
             <div class="databox box-shadow bg-white radius-bordered">
               <div class="databox-left bg-yellow-deep">
                 <div class="databox-piechart">
                  	<div class="easypiechart" data-percent="0" data-size="47" data-animate="500">
               			 		<span class="white font-90" data-epc-percent="本月跟进客户">0%</span>
                  	</div>
                 </div>
               </div>
               <div class="databox-right">
                 <span class="databox-number yellow-deep" data-epc-count="本月跟进客户">0</span>
                 <div class="databox-text darkgray">本月跟进客户</div>
                 <div class="databox-stat yellow-deep radius-bordered">
                  	<i class="stat-icon icon-lg fa fa-tasks"></i>
                 </div>
               </div>
             </div>
       		</div>
       		<!-- 本月诺坊客户 -->
       		<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
             <div class="databox box-shadow bg-white radius-bordered">
               <div class="databox-left bg-blue-light">
                 <div class="databox-piechart">
                  	<div class="easypiechart" data-percent="0" data-size="47" data-animate="500">
               			 		<span class="white font-90" data-epc-percent="本月诺访客户">0%</span>
                  	</div>
                 </div>
               </div>
               <div class="databox-right">
                 <span class="databox-number blue-light" data-epc-count="本月诺访客户">0</span>
                 <div class="databox-text darkgray">本月诺访客户</div>
                 <div class="databox-stat blue-light radius-bordered">
                  	<i class="stat-icon icon-lg fa fa-tasks"></i>
                 </div>
               </div>
             </div>
       		</div>
       		<!-- 本月签约客户 -->
       		<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
             <div class="databox box-shadow bg-white radius-bordered">
               <div class="databox-left bg-palegreen">
                 <div class="databox-piechart">
                  	<div class="easypiechart" data-percent="0" data-size="47" data-animate="500">
               			 		<span class="white font-90" data-epc-percent="本月签约客户">0%</span>
                  	</div>
                 </div>
               </div>
               <div class="databox-right">
                 <span class="databox-number palegreen" data-epc-count="本月签约客户">0</span>
                 <div class="databox-text darkgray">本月签约客户</div>
                 <div class="databox-state bg-palegreen">
                    	<i class="fa fa-check"></i>
                    </div>
               </div>
             </div>
       		</div>
     		</div>
     		
     		<div class="row">
     			<div class="col-lg-9 col-md-9 col-sm-9 col-xs-12">
     				<div class="row">
     					<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
	      				<div class="widget">
	                <div class="widget-header bordered-bottom bordered-orange">
	                  <i class="widget-icon fa fa-tasks orange"></i>
	                  <span class="widget-caption orange">本月消课量</span>
	                </div>
	                <div class="widget-body">
	                	<table data-id-field="id" 
												 	 data-unique-id="id" 
													 data-check-on-init="true" 
													 data-click-to-select="true" 
													 data-mobile-responsive="true" id="table-amount" class="bootstrap-table">
											<thead>
												<tr>
													<th data-field="totalAmount" 		data-align="center" >总签约额</th>
													<th data-field="usedAmount" 		data-align="center" >已耗课时</th>
													<th data-field="surplusAmount" 	data-align="center" >剩余费用</th>
												</tr>
											</thead>
										</table>
	                </div>
              	</div>
	      			</div>
     				</div>
     				
     				<div class="row">
     					<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
								<div class="databox databox-lg databox-inverted radius-bordered databox-shadowed databox-graded databox-vertical">
									<div class="databox-top bg-orange no-padding">
										<div class="horizontal-space space-lg"></div>
										<div class="databox-sparkline no-margin">
											<span class="sparkline" data-sparkline="compositebar" data-year-counts="全年诺访数" data-values="[0,0,0,0,0,0,0,0,0,0,0,0]"></span>
										</div>
									</div>
									<div class="databox-bottom no-padding">
										<div class="databox-row">
											<div class="databox-cell cell-6 text-align-left">
												<span class="databox-text">全年诺访数</span> <span
													class="databox-number" data-year-count="全年诺访数">0</span>
											</div>
											<div class="databox-cell cell-6 text-align-right">
												<span class="databox-text">当月诺访数</span> <span
													class="databox-number font-70" data-month-count="全年诺访数">0</span>
											</div>
										</div>
									</div>
								</div>
							</div>
							<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
								<div class="databox databox-lg databox-inverted radius-bordered databox-shadowed databox-graded databox-vertical">
									<div class="databox-top bg-blue-light no-padding">
										<div class="horizontal-space space-lg"></div>
										<div class="databox-sparkline no-margin">
											<span class="sparkline" data-sparkline="compositebar" data-year-counts="全年到访数" data-values="[0,0,0,0,0,0,0,0,0,0,0,0]"></span>
										</div>
									</div>
									<div class="databox-bottom no-padding">
										<div class="databox-row">
											<div class="databox-cell cell-6 text-align-left">
												<span class="databox-text">全年到访数</span> <span
													class="databox-number" data-year-count="全年到访数">0</span>
											</div>
											<div class="databox-cell cell-6 text-align-right">
												<span class="databox-text">当月到访数</span> <span
													class="databox-number font-70" data-month-count="全年到访数">0</span>
											</div>
										</div>
									</div>
								</div>
							</div>
							<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
								<div class="databox databox-lg databox-inverted radius-bordered databox-shadowed databox-graded databox-vertical">
									<div class="databox-top bg-palegreen no-padding">
										<div class="horizontal-space space-lg"></div>
										<div class="databox-sparkline no-margin">
											<span class="sparkline" data-sparkline="compositebar" data-year-counts="全年签约数" data-values="[0,0,0,0,0,0,0,0,0,0,0,0]"></span>
										</div>
									</div>
									<div class="databox-bottom no-padding">
										<div class="databox-row">
											<div class="databox-cell cell-6 text-align-left">
												<span class="databox-text">全年签约数</span> <span
													class="databox-number" data-year-count="全年签约数">0</span>
											</div>
											<div class="databox-cell cell-6 text-align-right">
												<span class="databox-text">当月签约数</span> <span
													class="databox-number font-70" data-month-count="全年签约数">0</span>
											</div>
										</div>
									</div>
								</div>
							</div>
     				</div>
     				
     				<div class="row">
     					<div class="col-xs-12">
     						<div class="dashboard-box radius-bordered">
									<div class="box-header">
										<div class="headline">本月客户情况</div>
									</div>
									<div class="box-tabs">
										<div class="tabable">
											<ul class="nav nav-tabs tabs-flat nav-justified">
												<li class="active">
													<a data-toggle="tab" href="#tab-salescustomer" onclick="handlerStatus(this, '0');" 			 data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true"> 新增的 </a>
												</li>
												<li>
													<a data-toggle="tab" href="#tab-salescustomer" onclick="handlerStatus(this, 'follows');" data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true"> 跟进的 </a>
												</li>
												<li>
													<a data-toggle="tab" href="#tab-salescustomer" onclick="handlerStatus(this, 'promise');" data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true"> 诺访的 </a>
												</li>
												<li>
													<a data-toggle="tab" href="#tab-salescustomer" onclick="handlerStatus(this, 'comming');" data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true"> 到店的 </a>
												</li>
												<li>
													<a data-toggle="tab" href="#tab-salescustomer" onclick="handlerStatus(this, 'losted');"  data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true"> 流失的 </a>
												</li>
											</ul>
											<div class="tab-content tabs-flat">
												<div class="tab-pane active" id="tab-salescustomer">
													<table data-ajax="handlerSearch" 
							           				 data-method="post" 
							           				 data-id-field="id" 
							           				 data-unique-id="id" 
							           				 data-pagination="true" 
							           				 data-check-on-init="true" 
							           				 data-click-to-select="true" 
							           				 data-side-pagination="server" 
							           				 data-mobile-responsive="true" 
							           				 data-event-onload="handlerSuccess" 
							           				 data-form-search="#search-form-customer" id="table-customer" class="bootstrap-table">
														<thead>
															<tr>
																<th data-field="id"						data-visible="false" data-switchable="false">ID</th>
																<th data-field="username" 		data-align="center" data-sortable="true">顾问</th>
																<th data-field="student" 			data-align="center" >宝宝</th>
																<th data-field="relation" 		data-align="center" >家长</th>
																<th data-field="mobile" 			data-align="center" >联系方式</th>
																<th data-field="market" 			data-align="center" data-sortable="true">渠道</th>
																<th data-field="source" 			data-align="center" data-sortable="true">来源</th>
																<th data-field="intention" 		data-align="center" data-sortable="true" data-formatter="renderIntention">意向</th>
																<th data-field="operator" 		data-align="center" data-width="130" data-formatter="renderOperator" data-table-target="customer">操作</th>
															</tr>
														</thead>
													</table>
												</div>
											</div>
										</div>
									</div>
								</div>
     					</div>
     				</div>
     			</div>
     			
     			<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
     				<div class="row ">
     					<div class="col-xs-12">
     						<div class="dashboard-box radius-bordered">
									<div class="box-header bordered-bottom">
										<div class="headline">本月销售排行</div>
									</div>
									<div class="box-content">
										<ul class="ranking-list">
                       <!-- dynamic data -->
                   	</ul>
									</div>
								</div>
     					</div>
     				</div>
     			</div>
     		</div>
     	</div>
   	</div>
	</div>
	
	<!-- form editor wrapper -->
	<div class="editor-wrapper">
		
	</div>
</div>
<!-- PAGE CONTENT END =============================================== -->

<!-- 查询表单 ========================================================= -->
<form id="search-form-customer" action="/miwawa/{dbname}/common/search.json" method="post" 
			name="com.app.miwawa.api.esm.entity.view.CustomerView">
			<input type="hidden" name="isparent" value="true" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="status" value="0" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="invalid" value="F" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="createDate" data-search-group="and" data-search-operation="BETWEEN" data-search-values="getRangeMonth" data-search-type="DATES">
			<input type="hidden" data-search-sort="createDate" data-search-order="desc">
</form>

<!-- 删除表单 ========================================================= -->
<form id="reback-form-customer" action="/miwawa/{dbname}/customer/reback" method="post" name="com.app.miwawa.api.esm.entity.customer.Customer"></form>
<form id="remove-form-customer" action="/miwawa/{dbname}/common/remove" 	method="post" name="com.app.miwawa.api.esm.entity.customer.Customer"></form>
<form id="remove-form-selling" 	action="/miwawa/{dbname}/common/remove" 	method="post" name="com.app.miwawa.api.esm.entity.customer.Selling"></form>
<form id="remove-form-bespoke" 	action="/miwawa/{dbname}/common/remove" 	method="post" name="com.app.miwawa.api.esm.entity.customer.Bespoke"></form>


<!-- Javascript ===================================================== -->
<script src="/miwawa/resources/lib/miwawa/1.0.2/js/core/requirejs/require.js"></script>

<script type="text/javascript">
<!--
	var $moment;
	var promisedate;
	
	jQuery(function(){
		require([
			'/miwawa/resources/lib/miwawa/1.0.2/js/core/module/app/simple.js?'+Date.parse(new Date()),
			'/miwawa/resources/lib/miwawa/1.0.2/js/core/module/app/dashboard.js?'+Date.parse(new Date()),
			'moment'
		], function(simple,dashboard,moment){
			dashboard.leaders();
			simple(['registerForm','registerWidget']);
			$moment = moment;
		});
		jQuery(":hidden[name='userid']","#search-form-customer").val(userid);
	});
	
	function handlerSwitch(trigger, method, key){
		jQuery(trigger).ajaxSearch({
			searchSuccess: function(data, status, xhr){
				if(data && data.rows ) {
					jQuery("#table-"+method).bootstrapTable('refreshOptions', {columns: columns[key]});
					createData[method](data.rows);
				}
			}
		});
	}
	
	// 渲染列表意向程度列
	var intention = ["","差","较差","一般","强","非常强"]
	function renderIntention(value, row, index){
		return row.id != "new" ? value ? intention[value] : "未跟进" : value;
	}
	
	function renderOperator(value,row,index){
		 var operator = "";
		switch(this.tableTarget){
			case "customer":
				if(row.status == "losted"){
					operator +="<a class='btn btn-info 	  btn-xs btn-table-op' onclick='handlerCreate(this,\""+row.id+"\")' data-table-source='#table-customer' data-editor-path='/miwawa/content/portal/customer/editor' data-editor-page='view.html' data-toggle='tooltip' data-placement='top' title='查看'> <i class='icon-call-out'></i> </a>" 
										+"<a class='btn btn-success btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='还原' onclick='handlerReback(this, \""+row.customerid+"\")' data-table-source='#table-customer' data-form-source='#reback-form-customer' data-custom='true'> <i class='fa fa-reply'></i> </a>"
									  +"<a class='btn btn-danger  btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='删除' onclick='handlerRemove(this, \""+row.customerid+"\")' data-table-source='#table-customer' data-form-source='#remove-form-customer'> <i class='fa fa-trash'></i> </a>";
				}else{
					operator = "<a class='btn btn-info 	  btn-xs btn-table-op' onclick='handlerCreate(this,\""+row.id+"\")' data-table-source='#table-customer' data-editor-path='/miwawa/content/portal/customer/editor' data-editor-page='edit.html' data-toggle='tooltip' data-placement='top' title='查看与跟进'> <i class='icon-call-out'></i> </a>"
									 + "<a class='btn btn-success btn-xs btn-table-op' onclick='handlerCreate(this,\""+row.id+"\")' data-table-source='#table-customer' data-editor-path='/miwawa/content/portal/customer/editor' data-editor-page='sign.html' data-toggle='tooltip' data-placement='top' title='报名与签约'> <i class='icon-loop'></i> </a>";
				}
				
				if(row.status == "comming"){
					operator+= "<a class='btn btn-warning  btn-xs btn-table-op' onclick='followAgain(\""+row.bespokeDate+"\",\""+row.promiseDate+"\",\""+row.customerid+"\")' data-toggle='tooltip' data-placement='top' title='重新跟进'> <i class='fa fa-undo'></i> </a>";
				}
				
				if(row.status != "losted" && row.status != "member"){
					operator+= "<a class='btn btn-danger  btn-xs btn-table-op' onclick='handlerCreate(this,\""+row.id+"\")' data-table-source='#table-customer' data-editor-path='/miwawa/content/portal/customer/editor' data-editor-page='lost.html' data-toggle='tooltip' data-placement='left' title='客户流失'> <i class='icon-user-unfollow'></i> </a>";
				}
			break;
			case "selling":
				if(row.id != "new"){
					operator = "<a class='btn btn-danger btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='删除' onclick='handlerRemove(this, \""+row.id+"\")' data-table-source='#table-selling' data-form-source='#remove-form-selling'> <i class='fa fa-trash'></i> </a>";
				}else{
					operator = value;
				}
			break;
			case "bespoke":
				if(row.id != "new"){
					operator = "<a class='btn btn-danger btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='删除' onclick='handlerRemove(this, \""+row.id+"\")' data-table-source='#table-bespoke' data-form-source='#remove-form-bespoke'> <i class='fa fa-trash'></i> </a>";
				}else{
					operator = value;
				}
			break;
		}
		return operator; 
	}
	
	// 列表字段定义
	var columns = {
		"0":[
			{ field: 'id', 					title: 'ID',	visible: false, switchable:false }, 
			{ field: 'username', 		title: '顾问' },
			{ field: 'student', 		title: '宝宝' },
			{ field: 'relation', 		title: '家长' },
			{ field: 'mobile', 			title: '联系方式' },
			{ field: 'market', 			title: '渠道' },
			{ field: 'source', 			title: '来源' },
			{ field: 'intention', 	title: '意向' },
			{ field: 'operator', 		title: '操作' }		
		],
		"follows":[
			{ field: 'id', 					title: 'ID',	visible: false, switchable:false }, 
			{ field: 'username', 		title: '顾问' },
			{ field: 'student', 		title: '宝宝' },
			{ field: 'relation', 		title: '家长' },
			{ field: 'mobile', 			title: '联系方式' },
			{ field: 'follows', 		title: '跟进次数' },
			{ field: 'lastDate', 		title: '最近跟进' },
			{ field: 'intention', 	title: '意向' },
			{ field: 'operator', 		title: '操作' }		
		],
		"promise":[
			{ field: 'id', 					title: 'ID',	visible: false, switchable:false }, 
			{ field: 'username', 		title: '顾问' },
			{ field: 'student', 		title: '宝宝' },
			{ field: 'relation', 		title: '家长' },
			{ field: 'mobile', 			title: '联系方式' },
			{ field: 'bespoke', 		title: '诺坊次数' },
			{ field: 'promiseDate', title: '最近诺坊' },
			{ field: 'bespokeType', title: '诺坊类型', formatter:function(value){return value}},
			{ field: 'operator', 		title: '操作' }		
		],
		"comming":[
			{ field: 'id', 					title: 'ID',	visible: false, switchable:false }, 
			{ field: 'username', 		title: '顾问' },
			{ field: 'student', 		title: '宝宝' },
			{ field: 'relation', 		title: '家长' },
			{ field: 'mobile', 			title: '联系方式' },
			{ field: 'promiseDate', title: '最近诺坊' },
			{ field: 'commingDate', title: '最近到店' },
			{ field: 'comming', 		title: '到店次数', formatter:function(value){return value} },
			{ field: 'operator', 		title: '操作' }		
		],
		"losted":[
			{ field: 'id', 					title: 'ID',	visible: false, switchable:false }, 
			{ field: 'username', 		title: '顾问' },
			{ field: 'student', 		title: '宝宝' },
			{ field: 'relation', 		title: '家长' },
			{ field: 'mobile', 			title: '联系方式' },
			{ field: 'market', 			title: '渠道' },
			{ field: 'source', 			title: '来源' },
			{ field: 'lostedReason',title: '流失原因', formatter:function(value){return value}},
			{ field: 'operator', 		title: '操作' }	
		],
		"member":[
			{ field: 'id', 					title: 'ID',	visible: false, switchable:false }, 
			{ field: 'username', 		title: '顾问' },
			{ field: 'student', 		title: '宝宝' },
			{ field: 'relation', 		title: '家长' },
			{ field: 'mobile', 			title: '联系方式' },
			{ field: 'market', 			title: '渠道' },
			{ field: 'source', 			title: '来源' },
			{ field: 'follows', 		title: '跟进次数', formatter:function(value){return value} },
			{ field: 'operator', 		title: '操作' }		
		]
	};
	
	function tooltipContent(value, row, index){
		return "<a style='cursor:pointer' data-toggle='tooltip' data-placement='top' title='"+row.content+"'>"+value+"</a>";
	}	
	
	/* ===================================== */
	/* 新增及编辑页面 */
	/* ===================================== */
	
	// 新增宝宝输入表单后的回调
	function handlerParents(options){
		if(options.id){
			var last = jQuery(".form-editor:last", ".editor-body-parents");
			last.append('<input type="hidden" name="id" value="'+options.id+'"/>');
			last.data("entity","com.app.miwawa.api.esm.entity.customer.Parents");
		}else{
			require(['jquery.plugin.form'],function(form){
				var relation = jQuery("#relation",".form-editor-parents-"+options.index);
				form.chosen(relation).select(relation);
				form.tooltip("[data-toggle='tooltip']");
			});
		}
	}
	
	// 新增宝宝输入表单后的回调
	function handlerStudent(options){
		if(options.id){
			// 更新表单信息
			var last = jQuery(".form-editor:last", ".editor-body-student");
			last.append('<input type="hidden" name="id" value="'+options.id+'"/>');
			last.data("entity","com.app.miwawa.api.esm.entity.customer.Student");
			
			// 更新头像信息
			var avatar = last.find(":hidden[name='avatar']").next("a");
			avatar.data("update",true);
			avatar.data("pk",options.id);
			avatar.data("entity","com.app.miwawa.api.esm.entity.customer.Student");
		}else{
			require(['jquery.plugin.form'],function(form){
				form.datepicker(jQuery(".date-selector",".form-editor-student-"+options.index));
				form.tooltip("[data-toggle='tooltip']");
			});
		}
	}
	
	// 新增地址输入表单后的回调
	function handlerAddress(options){
		if(options.id){
			// 更新表单信息
			var last = jQuery(".form-editor:last", ".editor-body-address");
			last.find(":hidden[name='id']").val(options.id);
		}
	}
	
	// 打开模态窗口
	function handlerModal(trigger, url, before, callback){
		jQuery("#extension-modal").removeClass("modal-middle").data("url",url).data("onshow",function(){
			if(before) window[before](trigger);
		}).data("onhidden",function(){
			if(callback) window[callback](trigger);
		}).modal("show");
	}
	
	// 试听排班模态窗口
	function handlerAudition(trigger, rowid, promise){
		promisedate = promise;	
		jQuery("#extension-modal").removeClass("modal-middle").data("selectedRow", jQuery("#table-bespoke").bootstrapTable("getRowByUniqueId",rowid))
															.data("url","content/esm/customer/dialog/classes.html").data("onshow",function(){
																var $table = jQuery("#table-classes");
																if($table.hasClass("table")){
																	$table.bootstrapTable("refresh");
																}else{
																	$table.bootstrapTable();
																}
															}).modal("show");
	}
	
	// 到店
	function handlerComming(trigger, rowid){
		require(["moment"],function(moment){
			jQuery.ajaxsend({
				url: "/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
				data: {
					"entityClass":"com.app.miwawa.api.esm.entity.customer.Bespoke",
					"bean":{"id":rowid,
									"commingDate":moment().format("YYYY-MM-DD HH:mm"),
									"commingState":"0"
					       }
				},
				success:function(data){
					if(data && !data.error) {
						jQuery("[data-toggle='tooltip']","#table-bespoke").tooltip();
						jQuery("#table-bespoke").bootstrapTable("refresh");
					}
				}
			});
		});
	}
	
	// 未到店
	function notTo(trigger, rowid){
		require(["moment"],function(moment){
			jQuery.ajaxsend({
				url: "/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
				data: {
					"entityClass":"com.app.miwawa.api.esm.entity.customer.Bespoke",
					"bean":{"id":rowid, "commingState":"1"}
				},
				success:function(data){
					if(data && !data.error) {
						jQuery("[data-toggle='tooltip']","#table-bespoke").tooltip();
						jQuery("#table-bespoke").bootstrapTable("refresh");
					}
				}
			});
		});
	}
	
	// 渲染列表试听列
	function renderAudition(value, row, index){
		if(value == "试听" && row.classes){
			return value+" "+row.classes;
		}else if(value == "试听" && row.id != "new" && row.commingState == ""){
			return value+" <a class='btn btn-success btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='试听' onclick='handlerAudition(this, \""+row.id+"\", \""+row.promiseDate+"\")'> <i class='icon-earphones'></i> </a>";			
		}else if(value == "试听" && row.commingState != ""){
			return value;
		}else{
			return value;			
		}
	}
	
	// 渲染列表到访列
	function renderComming(value, row, index){
		if(row.commingState == "" ){
			return "<a class='btn btn-success btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='到店' onclick='handlerComming(this, \""+row.id+"\")'> <i class='fa fa-check'></i> </a>&nbsp;到店"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='btn btn-warning btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='到店' onclick='notTo(this, \""+row.id+"\")'> <i class='fa fa-times'></i> </a>&nbsp;未到店";			
		}else if(row.commingState == "1"){
			return "未到店";			
		}else if(row.commingState == "0"||row.commingState == "2"){
			return "已到店";			
		}else{
			return "";
		}
	}
	
	/* ===================================== */
	/* 表格 行编辑 开始 */
	/* ===================================== */
	// 添加行编辑
	function handlerAddRow(target){
		var $table = jQuery(target);
		
		if(!hasEditorRow()){
			var rowdata = {
				"table-selling":{
					id:'new',
	        createDate: createInput('createDate', 'text', 		'ext-time'),
	        intention: 	createInput('intention', 	'text', 		'ext-rate', '3'),
	        content: 		createInput('content', 		'text', 		'ext-text'),
	        nextDate: 	createInput('nextDate', 	'text', 		'ext-date', $moment().add(3, 'd').format("YYYY-MM-DD HH:mm")),
	        username:		createInput('username', 	'readonly', 'ext-text', username),
	        operator: 	createButton(target)
				},
				"table-bespoke":{
					id:'new',
          createDate: 	createInput('createDate', 	'text', 		'ext-time'),
          bespokeType: 	createInput('bespokeType', 	'select', 	'ext-text'),
          promiseDate: 	createInput('promiseDate',	'text', 		'ext-date'),
          username:			createInput('username', 		'readonly', 'ext-text', username),
          operator: 		createButton(target)
				}
			};
			
			$table.bootstrapTable('insertRow', { index: 0, row: rowdata[target.replace("#","")] });
			
			setTimeout(function(){
				require(['jquery.plugin.form'],function(form){
					form.rating(".ext-rate");
					form.datepicker(".ext-time",{format: "YYYY-MM-DD HH:mm"});
					form.datepicker(".ext-date",{format: "YYYY-MM-DD HH:mm",defaultDate:""});
				});
			},0);
		}
		
		// 创建输入框
		function createInput(name, type, ext, value){
			switch(type){
				case 'text': 
					var input = "";
					if(value != undefined){
						input = '<input name="'+name+'" type="'+type+'" id="'+name+'" class="form-control '+ext+'" value="'+value+'">';
					}else{
						input = '<input name="'+name+'" type="'+type+'" id="'+name+'" class="form-control '+ext+'">';
					}
				return input;
				case 'readonly': 
					input = '<input name="'+name+'" type="'+type+'" class="form-control '+ext+'" value="'+value+'" readonly="readonly">';
					if(name=="username"){
						input+='<input name="userid" type="hidden" value="'+userid+'">';
					}
				return input;
				case 'select': 
					var select='<select name="'+name+'" id="'+name+'" class="form-control" style="width:80px;">'
									  +	'<option value="看店">看店</option>'
									  +	'<option value="试听">试听</option>'
									  +	'<option value="签约">签约</option>'
									  +	'<option value="付款">付款</option>'
									  +'</select>';
				return select;
			}
		}
		
		// 创建按钮
		function createButton(target){
			return ""
						+"<span class='table-editor-tools'>"
						+"	<button type='button' class='btn btn-danger  btn-xs' onclick='handlerCancelRow(\""+target+"\");'><i class='icon ion-close-circled'></i></button>"
						+"	<button type='button' class='btn btn-success btn-xs' onclick='handlerSubmitRow(\""+target+"\",\"validated\");'><i class='icon ion-checkmark-circled'></i></button>"
						+"</span>";
		}
		
		// 判断是否有编辑行
		function hasEditorRow(){
			return $table.bootstrapTable('getRowByUniqueId', 'new') ? true : false;
		}
	}
	
	//跟进输入框必填
	function validated(){
		if(jQuery("#content").val()!=undefined){
			if(jQuery("#createDate",".table-editor-selling").val()==""){
				jQuery.message.alert("请输入通话时间！");
				return false;
			}else if(jQuery("#content").val()==""){
				jQuery.message.alert("请输入跟踪情况！");
				return false;
			}else if(jQuery("#content").val().length > 100){
				jQuery.message.alert("跟踪情况字数限制在100以内！");
				return false;
			}else if(jQuery("#nextDate").val()==""){
				jQuery.message.alert("请输入下次沟通时间！");
				return false;
			}else{
				return true;			
			}
		}else{
			if(jQuery("#createDate",".table-editor-bespoke").val()==""){
				jQuery.message.alert("请输入预约时间！");
				return false;
			}else if(jQuery("#promiseDate").val()==""){
				jQuery.message.alert("请输入诺访时间！");
				return false;
			}else{
				var selling = [];
				var customer = jQuery("#table-customer").data("selectedRow");
				selling.push({
					"content":jQuery("#bespokeType").val(),
					"intention":"5",
					"createDate":jQuery("#createDate",".table-editor-bespoke").val(),
					"nextDate":jQuery("#promiseDate").val(),
					"userid":customer.userid,
					"username":customer.username,
					"customerId":customer.customerid,
					"status":"0",
					"invalid":"F"
				});
				jQuery.ajaxsend({
					url:"/miwawa/{dbname}/common/save".replace("{dbname}",dbname),
					data:{
						"entityClass":"com.app.miwawa.api.esm.entity.customer.Selling",
						"beans":selling
					},
					success:function(data){
						jQuery("#table-bespoke").bootstrapTable('refresh');
						jQuery("#table-selling").bootstrapTable('refresh');
						return true;		
					}
				});
			}
		}
	}
	
	function followAgain(createDate, promiseDate ,customerId){
		jQuery.ajaxsend({
			url : "/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
			data : {
	       		"entityClass" : "com.app.miwawa.api.esm.entity.customer.Bespoke",
	          "and[0][operator]": "EQ",
	          "and[0][fieldName]": "customerId",					
	          "and[0][fieldValue]": customerId,
	          "order[0][sort]": "promiseDate",
	          "order[0][order]":"desc"
	  	},
	  	success : function(data) {
	  		var bespokeList = data.rows;
				jQuery.ajaxsend({
					url:"/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
					data:{
				       	"entityClass" : "com.app.miwawa.api.esm.entity.customer.Bespoke",
				       	"bean":{"id":bespokeList[0].id,"commingState":"2"}
				  },
				  success : function(data) {
				  	jQuery("#table-customer").bootstrapTable("refresh");
				  }
				});
	  	}
		});
	}
//-->
</script>
